package CircularQueueDeom;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-03-15
 * Time: 15:34
 */
public class CircularQueueSpace {
    public int[] elem;
    public int front;
    public int rear;
    public  CircularQueueSpace() {
        elem = new int[8];
    }
    //入队
    public void offer(int val) {
        if(isFull()) {
            throw new CircularQueueSizeFullException("队满异常！！！");
        } else {
            elem[rear] = val;
            rear = (rear+1) % elem.length;
        }
    }
    //出队
    public int poll() {
        //1.判断队空不空
        if(isEmpty()) {
            //1.1队空
            throw new CircularQueueSizeEmptyException("循环队列空异常！！！");
        } else {
            //1.2队不空
            int val = elem[front];
            front = (front+1) % elem.length;
            return val;
        }
    }
    //判断队中是否空
    public boolean isEmpty() {
        return front == rear;
    }
    //判断循环队列已满
    public boolean isFull() {
        return front == (rear+1) % elem.length;
    }
    //Front 获取队头元素
    public int  Front () {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }
    //Rear 获取队尾元素
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int ret = (rear == 0) ? elem.length-1 : rear-1;
        return elem[ret];
    }
}
